/**
* MapsCast
* Applicast(tm) Widget
*
* (c) 2009 Estanislau Trepat / ILERRED, S.L.
*/

var Widget = Class.create({
  initialize: function() {
    // Initialization
    this.assetsPath = './assets/';

    // Important!
    // Remember to enter your google static maps API key here...
    AppliMaps_Config.setKey('PUT YOUR GOOGLE STATIC MAPS API KEY HERE');

    this.options = {
      lastQuery: '',
      defaultLocation: '',
      fullscreen: false
    };
    Object.extend(this.options, arguments[0] || { });
    this.loadRegistryOptions();

    this.stateNodes = [$('state-error'), $('state-loading'), $('state-normal'), $('state-active')];
    if ( this.options.fullscreen )
      this.stateNodes.push($('state-fullscreen'));

    this.view = null;

    // Core events
    Event.observe('keypress', this.onKeyPress.bind(this));
    Event.observe('modechange', this.onModeChange.bind(this));

    // First run check
    this.firstRun = this.firstRunCheck();

    // Launch appropiate view controller
    if ( this.firstRun ) {
      this.changeMode(WidgetMode.NORMALFOCUS);
      this.view = new MapsCastViewFirstRun({assetsPath: this.assetsPath});
    } else {
      if ( this.options.fullscreen ) {
        this.changeMode(WidgetMode.FULLSCREEN);
        this.view = new MapsCastViewFullScreen({assetsPath: this.assetsPath, address: this.options.lastQuery});
      } else {
        this.changeMode(WidgetMode.NORMALFOCUS);
        this.view = new MapsCastViewNormal({assetsPath: this.assetsPath, address: this.options.lastQuery});
      }
    }
  },

  firstRunCheck: function() {
    return ( (this.options.lastQuery === '') ? true : false );
  },

  loadRegistryOptions: function() {
    var lastQuery = getRegistry('Item1');
    if ( lastQuery )
      this.options.lastQuery = String(lastQuery);

    var defaultLocation = getStoredValue('Item1');
    if ( defaultLocation )
      this.options.defaultLocation = String(defaultLocation);
  },

  changeMode: function(newMode) {
    this.currentMode = newMode;
    this.setupComponent(newMode);
    this.showComponent(newMode);
  },

  showComponent: function(index) {
    if ( index < 0 || index >= this.stateNodes.length ) return;

    for(var i=0, m=this.stateNodes.length; i < m; ++i) {
      if ( i == index )
        this.stateNodes[i].setVisible(true);
      else
        this.stateNodes[i].setVisible(false);
    }
  },

  setupComponent: function(index) {
    if ( index < 0 || index >= WidgetModes.length ) return;

    switch(WidgetModes[index]) {
      case 'Error':
        $('bg-error').loadImage(this.assetsPath + 'backgrounds/bg-error.png');
        break;

      case 'Loading':
        break;

      case 'Normal/Focus':
        if ( this.firstRun )
          $('bg-normal').loadImage(this.assetsPath + 'backgrounds/bg-firstrun.png');
        else
          $('bg-normal').loadImage(this.assetsPath + 'backgrounds/bg-normal.png');
        break;

      case 'Active':
        $('bg-active').loadImage(this.assetsPath + 'backgrounds/bg-active.png');
        break;

      case 'Full-Screen':
        // no background needed
        break;
    }
  },

  onKeyPress: function(e) {
    if ( !e ) return;

    // Delegate keypress events to current view controller
    if ( this.view && typeof this.view.onKeyPress !== 'undefined' )
      this.view.onKeyPress(e);
  },

  onModeChange: function(e) {
    if ( !e ) return;

    switch( WidgetModes[e.mode] ) {
      case 'Normal/Focus':
        if ( this.view ) {
          if ( e.focus )
            this.view.enable();
          else
            this.view.disable();
        }
        break;

      case 'Active':
        this.changeMode(WidgetMode.ACTIVE);
        this.view = new MapsCastViewActive({assetsPath: this.assetsPath, address: this.options.lastQuery});
        break;
    }
  }
});